

--[[
title: filesystem
description: 文件系统
]]

local fs = {};

function fs.GetPathSeparator(platform)
    return commonlib.IsWindowPlatform(platform) and "\\" or "/";
end

function fs.GetFileText(filename)
    local fullpath = commonlib.GetFullPath(filename)

    if (commonlib.IsAssetFilePath(fullpath)) then return commonlib.GetAssetTextByFilePath(fullpath) end 
    
    if (commonlib.IsScriptFilePath(fullpath)) then return commonlib.GetScriptTextByFilePath(fullpath) end

    local file, errmsg = io.open(fullpath, "rb");
    
    if (not file) then
        print("ERROR: open file failed", filename, fullpath, errmsg);
        return nil;
    end

    local text = file:read("a");
    file:close();

    return text;
end

function fs.IsExistFile(filename)
    local file = io.open(filename, "rb");
    if (not file) then return false end 
    file:close();
    return true;
end

function fs.FormatPath(path, platform)
    if(commonlib.IsWindowPlatform(platform)) then
        path = string.gsub(path, "/+", "\\");
		path = string.gsub(path, "\\+", "\\");
	else
		path = string.gsub(path, "\\+", "/");
        path = string.gsub(path, "/+", "/");
	end
    return path;
end

commonlib.GetFileText = fs.GetFileText;

return fs;